package com.mc2.docapp.disabilityassessmenttool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_LL_muscle_power;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_llmp_ankleFoot;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_llmp_hip;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_llmp_knee;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.LLCustDataType;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.LLInnerCalculations;
import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;

public class LowLimbMusclePower extends Activity {

    private static final int id = Activities.LOWER_LIMB_MUSCLE_POWER;

    public C_llmp_hip c_llmp_hip = new C_llmp_hip();
    public C_llmp_knee c_llmp_knee = new C_llmp_knee();
    public C_llmp_ankleFoot c_llmp_ankleFoot = new C_llmp_ankleFoot();

    private EditText hipFlexionLeft;
    private EditText hipFlexionRight;
    private EditText hipExtensionLeft;
    private EditText hipExtensionRight;
    private EditText hipInternalRotationLeft;
    private EditText hipInternalRotationRight;
    private EditText hipExternalRotationLeft;
    private EditText hipExternalRotationRight;
    private EditText hipAdductionLeft;
    private EditText hipAdductionRight;
    private EditText hipAbductionLeft;
    private EditText hipAbductionRight;
    private EditText kneeFlexionLeft;
    private EditText kneeFlexionRight;
    private EditText kneeExtensionLeft;
    private EditText kneeExtensionRight;
    private EditText anklefootDorsiflexionLeft;
    private EditText anklefootDorsiflexionRight;
    private EditText anklefootPlantarflexionLeft;
    private EditText anklefootPlantarflexionRight;
    private EditText anklefootInversionLeft;
    private EditText anklefootInversionRight;
    private EditText anklefootEversionLeft;
    private EditText anklefootEversionRight;


    //db instance
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_low_limb_muscle_power);

        hipFlexionLeft = (EditText) findViewById(R.id.hipFlexionLeft);
        hipFlexionRight = (EditText) findViewById(R.id.hipFlexionRight);
        hipExtensionLeft = (EditText) findViewById(R.id.hipExtensionLeft);
        hipExtensionRight = (EditText) findViewById(R.id.hipExtensionRight);
        hipInternalRotationLeft = (EditText) findViewById(R.id.hipInternalRotationLeft);
        hipInternalRotationRight = (EditText) findViewById(R.id.hipInternalRotationRight);
        hipExternalRotationLeft = (EditText) findViewById(R.id.hipExternalRotationLeft);
        hipExternalRotationRight = (EditText) findViewById(R.id.hipExternalRotationRight);
        hipAdductionLeft = (EditText) findViewById(R.id.hipAdductionLeft);
        hipAdductionRight = (EditText) findViewById(R.id.hipAdductionRight);
        hipAbductionLeft = (EditText) findViewById(R.id.hipAbductionLeft);
        hipAbductionRight = (EditText) findViewById(R.id.hipAbductionRight);
        kneeFlexionLeft = (EditText) findViewById(R.id.kneeFlexionLeft);
        kneeFlexionRight = (EditText) findViewById(R.id.kneeFlexionRight);
        kneeExtensionLeft = (EditText) findViewById(R.id.kneeExtensionLeft);
        kneeExtensionRight = (EditText) findViewById(R.id.kneeExtensionRight);
        anklefootDorsiflexionLeft = (EditText) findViewById(R.id.anklefootDorsiflexionLeft);
        anklefootDorsiflexionRight = (EditText) findViewById(R.id.anklefootDorsiflexionRight);
        anklefootPlantarflexionLeft = (EditText) findViewById(R.id.anklefootPlantarflexionLeft);
        anklefootPlantarflexionRight = (EditText) findViewById(R.id.anklefootPlantarflexionRight);
        anklefootInversionLeft = (EditText) findViewById(R.id.anklefootInversionLeft);
        anklefootInversionRight = (EditText) findViewById(R.id.anklefootInversionRight);
        anklefootEversionLeft = (EditText) findViewById(R.id.anklefootEversionLeft);
        anklefootEversionRight = (EditText) findViewById(R.id.anklefootEversionRight);
        initRealmDB();
        loadPrevData();
        sanitizeInput();


        Button saveNnextButton = (Button) findViewById(R.id.saveAndNext);
        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //HIP
                c_llmp_hip.setFlexion(new LLCustDataType(Integer.parseInt(IfNull(hipFlexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(hipFlexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_hip.setExtension(new LLCustDataType(Integer.parseInt(IfNull(hipExtensionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(hipExtensionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_hip.setInternal_rotation(new LLCustDataType(Integer.parseInt(IfNull(hipInternalRotationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(hipInternalRotationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_hip.setExternal_rotation(new LLCustDataType(Integer.parseInt(IfNull(hipExternalRotationRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(hipExternalRotationLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_hip.setAdduction(new LLCustDataType(Integer.parseInt(IfNull(hipAdductionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(hipAdductionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_hip.setAbduction(new LLCustDataType(Integer.parseInt(IfNull(hipAbductionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(hipAbductionLeft.getText().toString(), "5")), 5d, 0, 0));


                c_llmp_hip.calc(c_llmp_hip);

                //Knee
                c_llmp_knee.setFlexion(new LLCustDataType(Integer.parseInt(IfNull(kneeFlexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(kneeFlexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_knee.setExtension(new LLCustDataType(Integer.parseInt(IfNull(kneeExtensionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(kneeExtensionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_knee.calc(c_llmp_knee);

                //ankle foot
                c_llmp_ankleFoot.setDorsiflexion(new LLCustDataType(Integer.parseInt(IfNull(anklefootDorsiflexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(anklefootDorsiflexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_ankleFoot.setPlantar_flexion(new LLCustDataType(Integer.parseInt(IfNull(anklefootPlantarflexionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(anklefootPlantarflexionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_ankleFoot.setInversion(new LLCustDataType(Integer.parseInt(IfNull(anklefootInversionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(anklefootInversionLeft.getText().toString(), "5")), 5d, 0, 0));

                c_llmp_ankleFoot.setEversion(new LLCustDataType(Integer.parseInt(IfNull(anklefootEversionRight.getText().toString(), "5")),
                        Integer.parseInt(IfNull(anklefootEversionLeft.getText().toString(), "5")), 5d, 0, 0));


                c_llmp_ankleFoot.calc(c_llmp_ankleFoot);

                C_LL_muscle_power c_ll_muscle_power = new C_LL_muscle_power();
                c_ll_muscle_power.setC_llmp_ankleFoot(c_llmp_ankleFoot);
                c_ll_muscle_power.setC_llmp_hip(c_llmp_hip);
                c_ll_muscle_power.setC_llmp_knee(c_llmp_knee);

                LLInnerCalculations innerCalculations = new LLInnerCalculations();
                innerCalculations.setSummation_left(c_ll_muscle_power.getC_llmp_hip().getInner_calculations().getSummation_left() +
                        c_ll_muscle_power.getC_llmp_knee().getInner_calculations().getSummation_left() +
                        c_ll_muscle_power.getC_llmp_ankleFoot().getInner_calculations().getSummation_left());

                innerCalculations.setSummation_right(c_ll_muscle_power.getC_llmp_hip().getInner_calculations().getSummation_right() +
                        c_ll_muscle_power.getC_llmp_knee().getInner_calculations().getSummation_right() +
                        c_ll_muscle_power.getC_llmp_ankleFoot().getInner_calculations().getSummation_right());

                c_ll_muscle_power.setInnerCalculations(innerCalculations);

                NewPatient.finalDto.getClass_lowerLimb_component().setLowerLimb_muscle_power(c_ll_muscle_power);

                calculateMobComponent(true);
                calculateMobComponent(false);

                //
                try {
//                    System.out.println(new ObjectMapper().writeValueAsString(c_active_rom));
                } catch (Exception e) {
                    }
                System.out.println();
                c_ll_muscle_power.setId(id);
                realm.beginTransaction();
                realm.deleteAll();
                realm.insert(c_ll_muscle_power);
                realm.commitTransaction();

                Intent intent = new Intent(LowLimbMusclePower.this, router(LowLimbMusclePower.class));
                startActivity(intent);

            }
        });

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });

    }

    public void calculateMobComponent(Boolean isLeft) {
        if(isLeft) {

            double a  = NewPatient.finalDto.getClass_lowerLimb_component().getLowerLimb_active_rom_degrees().getInnerCalculations().getSummation_left();
            double b = NewPatient.finalDto.getClass_lowerLimb_component().getLowerLimb_muscle_power().getInnerCalculations().getSummation_left();
            double step1;

            step1 = calByFormula(a,b);
            if(step1 > 90) {
                step1 = 90d;
            }
            NewPatient.finalDto.getClass_lowerLimb_component().setMobilityComponentLeft(step1);
        } else {
            double a  = NewPatient.finalDto.getClass_lowerLimb_component().getLowerLimb_active_rom_degrees().getInnerCalculations().getSummation_right();
            double b = NewPatient.finalDto.getClass_lowerLimb_component().getLowerLimb_muscle_power().getInnerCalculations().getSummation_right();
            double step1;

            step1 = calByFormula(a,b);
            if(step1 > 90) {
                step1 = 90d;
            }
            NewPatient.finalDto.getClass_lowerLimb_component().setMobilityComponentRight(step1);

        }

    }
    private double calByFormula(double a, double b) {
        if(a<b){
            a = a + b;
            b = a - b;
            a = a - b;
        }
        double c = a + (((90 - a) * b) / 90.0);
        return c;
    }


    public String IfNull(String value, String defaultValue) {
        if (value.isEmpty() || value == null) {
            return defaultValue;
        } else {
            return value;
        }
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        // realm = Realm.getDefaultInstance();
        RealmConfiguration config = new RealmConfiguration.Builder().name(id+".realm").build();
        realm = Realm.getInstance(config);
    }

    private void loadPrevData() {
        final RealmResults<C_LL_muscle_power> dtos = realm.where(C_LL_muscle_power.class).findAll();
//        .equalTo("id",id).findAll();
        System.out.println();
        try {
            //apply ui
            //
            //mapUI(new C_LL_muscle_power().strip(dtos.get(0)));
            //
            //realm.beginTransaction();
            //dtos.get(0).deleteFromRealm();
            //realm.commitTransaction();
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
            //no prev data found
        }
    }

    private void mapUI(C_LL_muscle_power oldData) {
        hipFlexionLeft.setText(String.valueOf((long) (oldData.getC_llmp_hip().getFlexion().getActual_left())));
        hipFlexionRight.setText(String.valueOf((long) (oldData.getC_llmp_hip().getFlexion().getActual_right())));

        hipExtensionLeft.setText(String.valueOf((long) (oldData.getC_llmp_hip().getExtension().getActual_left())));
        hipExtensionRight.setText(String.valueOf((long) (oldData.getC_llmp_hip().getExtension().getActual_right())));

        hipInternalRotationLeft.setText(String.valueOf((long) (oldData.getC_llmp_hip().getInternal_rotation().getActual_left())));
        hipInternalRotationRight.setText(String.valueOf((long) (oldData.getC_llmp_hip().getInternal_rotation().getActual_right())));

        hipExternalRotationLeft.setText(String.valueOf((long) (oldData.getC_llmp_hip().getExternal_rotation().getActual_left())));
        hipExternalRotationRight.setText(String.valueOf((long) (oldData.getC_llmp_hip().getExternal_rotation().getActual_right())));

        hipAdductionLeft.setText(String.valueOf((long) (oldData.getC_llmp_hip().getAdduction().getActual_left())));
        hipAdductionRight.setText(String.valueOf((long) (oldData.getC_llmp_hip().getAdduction().getActual_right())));

        hipAbductionLeft.setText(String.valueOf((long) (oldData.getC_llmp_hip().getAbduction().getActual_left())));
        hipAbductionRight.setText(String.valueOf((long) (oldData.getC_llmp_hip().getAbduction().getActual_right())));

        kneeFlexionLeft.setText(String.valueOf((long) (oldData.getC_llmp_knee().getFlexion().getActual_left())));
        kneeFlexionRight.setText(String.valueOf((long) (oldData.getC_llmp_knee().getFlexion().getActual_right())));

        kneeExtensionLeft.setText(String.valueOf((long) (oldData.getC_llmp_knee().getExtension().getActual_left())));
        kneeExtensionRight.setText(String.valueOf((long) (oldData.getC_llmp_knee().getExtension().getActual_right())));

        anklefootDorsiflexionLeft.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getDorsiflexion().getActual_left())));
        anklefootDorsiflexionRight.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getDorsiflexion().getActual_right())));

        anklefootPlantarflexionLeft.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getPlantar_flexion().getActual_left())));
        anklefootPlantarflexionRight.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getPlantar_flexion().getActual_right())));

        anklefootInversionLeft.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getInversion().getActual_left())));
        anklefootInversionRight.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getInversion().getActual_right())));

        anklefootEversionLeft.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getEversion().getActual_left())));
        anklefootEversionRight.setText(String.valueOf((long) (oldData.getC_llmp_ankleFoot().getEversion().getActual_right())));


    }


    public void sanitizeInput() {
        hipFlexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        hipFlexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        hipExtensionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        hipExtensionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        hipInternalRotationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        hipInternalRotationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        hipExternalRotationLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        hipExternalRotationRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        hipAdductionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        hipAdductionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        hipAbductionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        hipAbductionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        kneeFlexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        kneeFlexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        kneeExtensionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        kneeExtensionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        anklefootDorsiflexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        anklefootDorsiflexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        anklefootPlantarflexionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        anklefootPlantarflexionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        anklefootInversionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        anklefootInversionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

        anklefootEversionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});
        anklefootEversionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "5")});

    }
}
